package com.ds.dsm.view.config.menu.service;

import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.view.config.menu.GridMenuPopTree;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.annotation.PopTreeViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.bean.ApiClassConfig;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.gallery.CustomGalleryViewBean;
import com.ds.esd.custom.gallery.GalleryRowCmdBean;
import com.ds.esd.custom.gallery.enums.GalleryRowMenu;
import com.ds.esd.custom.grid.CustomGridViewBean;
import com.ds.esd.custom.grid.GridRowCmdBean;
import com.ds.esd.custom.grid.enums.GridMenu;
import com.ds.esd.custom.grid.enums.GridRowMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@RequestMapping(path = "/dsm/view/config/grid/")

public class GridMenuService {


    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "列表按钮")
    @RequestMapping("GridMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<GridMenuPopTree>> getGridMenuTree(String sourceClassName, String viewInstId, String methodName, String esdsearchpattern) {
        TreeListResultModel<List<GridMenuPopTree>> model = new TreeListResultModel<>();
        List<GridMenuPopTree> popTrees = new ArrayList<>();
        try {
            GridMenuPopTree menuPopTree = new GridMenuPopTree("gridMenu", "列表按钮");
            for (GridMenu menu : GridMenu.values()) {
                GridMenuPopTree item = new GridMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }

    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "列表工具栏")
    @RequestMapping("ToolBarsTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<GridMenuPopTree>> getToolBarsTree(String sourceClassName, String viewInstId, String methodName, String esdsearchpattern) {
        TreeListResultModel<List<GridMenuPopTree>> model = new TreeListResultModel<>();
        List<GridMenuPopTree> popTrees = new ArrayList<>();
        try {
            GridMenuPopTree menuPopTree = new GridMenuPopTree("gridMenu", "列表按钮");
            for (GridMenu menu : GridMenu.values()) {
                GridMenuPopTree item = new GridMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }

    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "列表按钮")
    @RequestMapping("RowMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<GridMenuPopTree>> getRowMenuTree(String sourceClassName, String viewInstId, String methodName, String esdsearchpattern) {
        TreeListResultModel<List<GridMenuPopTree>> model = new TreeListResultModel<>();
        List<GridMenuPopTree> popTrees = new ArrayList<>();
        try {
            GridMenuPopTree menuPopTree = new GridMenuPopTree("rowmenu", "列表按钮");
            for (GridRowMenu menu : GridRowMenu.values()) {
                GridMenuPopTree item = new GridMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }


    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "底部工具栏")
    @RequestMapping("BottomMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<GridMenuPopTree>> getBottomMenuTree(String sourceClassName, String viewInstId, String methodName, String esdsearchpattern) {
        TreeListResultModel<List<GridMenuPopTree>> model = new TreeListResultModel<>();
        List<GridMenuPopTree> popTrees = new ArrayList<>();
        try {
            GridMenuPopTree menuPopTree = new GridMenuPopTree("bottommenu", "底部工具栏");
            for (GridMenu menu : GridMenu.values()) {
                GridMenuPopTree item = new GridMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }


    @RequestMapping("addMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.treeSave, callback = {CustomCallBack.ReloadParent, CustomCallBack.Close})
    @ResponseBody
    public ResultModel<Boolean> addMenu(String sourceClassName, String methodName, String GridMenuTree, String BottomMenuTree, String ToolBarsTree, String RowMenuTree, String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,viewInstId);
            ApiClassConfig customESDClassAPIBean = classConfig.getSourceConfig();
            MethodConfig methodAPIBean = customESDClassAPIBean.getMethodByName(methodName);

            if (methodAPIBean.getView() instanceof CustomGridViewBean) {
                CustomGridViewBean customGridViewBean = (CustomGridViewBean) methodAPIBean.getView();
                if (ToolBarsTree != null && !ToolBarsTree.equals("")) {
                    String[] menuIds = StringUtility.split(ToolBarsTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            customGridViewBean.getToolBarMenu().add(GridMenu.valueOf(menuId));
                        }
                    }
                } else if (GridMenuTree != null && !GridMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(GridMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            customGridViewBean.getCustomMenu().add(GridMenu.valueOf(menuId));
                        }
                    }
                } else if (RowMenuTree != null && !RowMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(RowMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            if (customGridViewBean.getRowCmdBean() == null) {
                                customGridViewBean.setRowCmdBean(new GridRowCmdBean());
                            }
                            customGridViewBean.getRowCmdBean().getRowMenu().add(GridRowMenu.valueOf(menuId));
                        }
                    }
                } else if (BottomMenuTree != null && !BottomMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(BottomMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            customGridViewBean.getBottombarMenu().add(GridMenu.valueOf(menuId));
                        }
                    }
                }
            } else if (methodAPIBean.getView() instanceof CustomGalleryViewBean) {
                CustomGalleryViewBean customGalleryViewBean = (CustomGalleryViewBean) methodAPIBean.getView();
                if (ToolBarsTree != null && !ToolBarsTree.equals("")) {
                    String[] menuIds = StringUtility.split(ToolBarsTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            customGalleryViewBean.getToolBarMenu().add(GridMenu.valueOf(menuId));
                        }
                    }
                } else if (GridMenuTree != null && !GridMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(GridMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            customGalleryViewBean.getCustomMenu().add(GridMenu.valueOf(menuId));
                        }
                    }
                } else if (RowMenuTree != null && !RowMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(RowMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            if (customGalleryViewBean.getRowCmdBean() == null) {
                                customGalleryViewBean.setRowCmdBean(new GalleryRowCmdBean());
                            }
                            customGalleryViewBean.getRowCmdBean().getRowMenu().add(GalleryRowMenu.valueOf(menuId));
                        }
                    }
                } else if (BottomMenuTree != null && !BottomMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(BottomMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            customGalleryViewBean.getBottombarMenu().add(GridMenu.valueOf(menuId));
                        }
                    }
                }
            }


            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(classConfig);
        } catch (
                JDSException e)

        {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }


        return model;
    }


    @RequestMapping("delMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.delete, callback = {CustomCallBack.Reload})
    @ResponseBody
    public ResultModel<Boolean> delMenu(String sourceClassName, String methodName, String type, String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            ApiClassConfig customESDClassAPIBean = classConfig.getSourceConfig();
            MethodConfig methodAPIBean = customESDClassAPIBean.getMethodByName(methodName);

            if (methodAPIBean.getView() instanceof CustomGridViewBean) {
                CustomGridViewBean customGridViewBean = (CustomGridViewBean) methodAPIBean.getView();
                String[] menuIds = StringUtility.split(type, ";");
                for (String menuId : menuIds) {
                    GridMenu gridMenu = GridMenu.fromType(menuId);
                    if (gridMenu != null) {
                        customGridViewBean.getCustomMenu().remove(GridMenu.valueOf(menuId));
                        customGridViewBean.getToolBarMenu().remove(GridMenu.valueOf(menuId));
                        customGridViewBean.getBottombarMenu().remove(GridMenu.valueOf(menuId));
                    }
                    GridRowMenu rowMenu = GridRowMenu.fromType(menuId);
                    if (rowMenu != null) {
                        customGridViewBean.getRowCmdBean().getRowMenu().remove(GridRowMenu.valueOf(menuId));

                    }

                }
            } else if (methodAPIBean.getView() instanceof CustomGalleryViewBean) {
                CustomGalleryViewBean customGalleryViewBean = (CustomGalleryViewBean) methodAPIBean.getView();
                String[] menuIds = StringUtility.split(type, ";");
                for (String menuId : menuIds) {
                    GridMenu gridMenu = GridMenu.fromType(menuId);
                    if (gridMenu != null) {
                        customGalleryViewBean.getCustomMenu().remove(GridMenu.valueOf(menuId));
                        customGalleryViewBean.getToolBarMenu().remove(GridMenu.valueOf(menuId));
                        customGalleryViewBean.getBottombarMenu().remove(GridMenu.valueOf(menuId));
                    }
                    GridRowMenu rowMenu = GridRowMenu.fromType(menuId);
                    if (rowMenu != null) {
                        customGalleryViewBean.getRowCmdBean().getRowMenu().remove(GridRowMenu.valueOf(menuId));

                    }

                }
            }


            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(classConfig);
        } catch (JDSException e) {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }
        return model;
    }


}
